package jia.tools;

import jia.tools.StaticDiceRoller;
import org.dom4j.Node;
import org.dom4j.Document;


/**
 * **************************************************************************
 * Application:
 * Author: Erik Stell
 * Date: Sep 10, 2004
 * Time: 4:59:22 PM
 * Process:
 * **************************************************************************
 */
public class XMLUtils {

    public static String getNodeValue(String type, Document xmlDoc, int groupNumber){
        String retVal = new String();
        Node thisNode = null;

        
        int howMany = xmlDoc.selectNodes("/generator/"+type+"Elements/"+type+"[@"+type+"_id='"+groupNumber+"']/"+type+"_element").size();
        int roll = StaticDiceRoller.rolldSomething(howMany, 1);
        if (roll > 0) {
            Object[] trainingClassArray = xmlDoc.selectNodes("/generator/"+type+"Elements/"+type+"[@"+type+"_id='"+groupNumber+"']" +
                    "/"+type+"_element[@"+type+"_element_id='" + roll + "']").toArray();
            thisNode = (Node) trainingClassArray[0];
            retVal = thisNode.getStringValue();
        }

        return retVal.toLowerCase();

    }

    public static String getNodeValue(String type, Document xmlDoc, String groupName){
        String retVal = new String();
        Node thisNode = null;

        int howMany = xmlDoc.selectNodes("/generator/"+type+"Elements/"+type+"[@"+type+"_type='"+groupName+"']/"+type+"_element").size();
        int roll = StaticDiceRoller.rolldSomething(howMany, 1);
        if (roll > 0) {
            Object[] trainingClassArray = xmlDoc.selectNodes("/generator/"+type+"Elements/"+type+"[@"+type+"_type='"+groupName+"']" +
                    "/"+type+"_element[@"+type+"_element_id='" + roll + "']").toArray();
            thisNode = (Node) trainingClassArray[0];
            retVal = thisNode.getStringValue();
        }

        return retVal.toLowerCase();

    }
}
